<?php
/**
 * 
 * http类
 * @author ADMIN
 *
 */
class Http{
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $col
	 */
	public static function post($col=null,$defaultReurn=null)
	{
	    if ($col){
	        $v = trim(@$_POST[$col]);
	        if ($defaultReurn){
	            if (!$v) return $defaultReurn;
	        }
		    return $v;
	    }else {
	        return $_POST;
	    }
	}
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $col
	 */
	public static function get($col=null,$defaultReurn=null)
	{
	    if ($col){
	        $v = trim(@$_GET[$col]);
	        if ($defaultReurn){
	            if (!$v) return $defaultReurn;
	        }
		    return $v;
	    }else {
	        return $_GET;
	    }
	}
	
	public function setGet($k,$v){
	    $_GET[$k]=$v;
	}
	/**
	 * 
	 * POST 方式提交到指定URL
	 * @param string $url
	 * @param array $data
	 * @param unknown_type $optional_headers
	 * @throws Exception
	 */
	public static function postRequest($url, $data, $optional_headers = null)
	{
		$params = array(
			'http' => array(
				'method' => 'POST',
				'content' => http_build_query($data),
			));
		if ($optional_headers !== null) 
		{
			$params['http']['header'] = $optional_headers;
		}else{
			$params['http']['header'] = 'Content-type: application/x-www-form-urlencoded';			
		}
		$ctx = stream_context_create($params);
		$fp = @fopen($url, 'rb', false, $ctx);
		if (!$fp) 
		{
		   throw new Exception("Problem with $url");
		}
		$response = @stream_get_contents($fp);
		if ($response === false) 
		{
		   throw new Exception("Problem reading data from $url");
		}
		return $response;
	}
	
	/**
	 * 
	 * 提交文件$fileUrl到$url
	 * @param string $url
	 * @param string $fileUrl
	 * @param array $data
	 */
	public static function postFile($url,$fileUrl,$data=null){
		$fileHandle = fopen($fileUrl, "rb");
		$fileContents = stream_get_contents($fileHandle);
		fclose($fileHandle);
		if ($data!=''){
			$p='&'.http_build_query($data);
		}
		$params = array(
		    'http' => array
		    (
			    'method' => 'POST',
			    'header'=>"Content-Type: multipart/form-data\r\n",
			    'content' => $fileContents.$p
		    )
		);
		
		$ctx = stream_context_create($params);
		$fp = fopen($url, 'rb', false, $ctx); 
		$response = stream_get_contents($fp);
		return $response;
	}
	
	/**
	 * 
	 * 代理发送数据 ...
	 * @param string $daliIP
	 * @param string $dailiPort
	 * @param string $serverUrl
	 * @param string $type
	 */
	public static function daili($daliIP,$dailiPort='8080',$serverUrl,$type='tcp'){
		$dailiPort = ':'.$dailiPort;
		$opts = array('http' => array('proxy' => $type.'://127.0.0.1'.$dailiPort, 'request_fulluri' => true));
		$context = stream_context_create($opts);
		$data = file_get_contents($serverUrl, false, $context);
		return $data;
	}
	
	/**
	 * 
	 * SOCKET发送数据 ...
	 * @param string $server
	 * @param string $serPort
	 * @param array $data
	 * @param string $type
	 */
	function socketData ($server, $port, $data,$type='tcp') 
	{ 
		$data = $data."\n";
	    $service_port = $port;
		$address = $server;
		$commonProtocol = getprotobyname($type);
		$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
		socket_connect($socket, $address, $service_port);
		socket_write($socket, $data, strlen($data));
		$out = socket_read($socket, 1024);
		socket_close($socket);
		return str_replace("\n", '', $out);
	} 

	
}
